
	

jQuery(document).ready(function($) {
	getArticleSort();
	getSection();
	//初始化富本编辑器
	 var E = window.wangEditor
     var editor = new E('#div1', '#div2')  // 两个参数也可以传入 elem 对象，class 选择器
      //editor.customConfig.uploadImgServer = getContextPath()+"/backStage/addArticle" //上传图片
     editor.create();
	 $('#submitArrticle').click(function(event) {
	 	/* Act on the event */

	 		      			var identifyingCode = $('#code').val(); //验证码
							var addArticleURL=getContextPath()+"/backStage/addArticle";
							var articleName = $('#articleName').val(); //标题
							var articleSortID = $("#articlesort").find("option:selected").attr('sortArticleId'); //分组id
							var articleContent= editor.txt.html() ; //内容
							var typeId = $("#typeId").find("option:selected").attr('sectionId');  //所属栏目
							var articleType = "0"; //是否公开
								if($("#articleType").is(':checked')) {
							    articleType="1";
								}
								
								var fd = new FormData();
								if($("#file").get(0).files[0]==null){
									alert('封面不能为空');
									return ;
								}
							    fd.append('file',$("#file").get(0).files[0]);
							    fd.append('identifyingCode',identifyingCode);
							    fd.append('articleName',articleName);
							    fd.append('articleSortID',articleSortID);
							    fd.append('articleContent',articleContent);
							    fd.append('typeId',typeId);
							    fd.append('articleType',articleType);

							    $.ajax({
							        url: addArticleURL,
							        type: 'POST',
							        datatype: 'json',
							        data: fd,
							        cache:false,
							        traditional: true,
							        contentType: false,
							        processData: false,
							        success: function (data) {
							        	alert('发表成功');
							        },
							        error: function () {
							        	alert('数据有错')
							        }
							    });
	
	 });
});

//提交图片
function commitPic(){
    var E = window.wangEditor
    var editor = new E('#div1')

    // 配置服务器端地址
    editor.customConfig.uploadImgServer = '/upload'

    // 进行下文提到的其他配置
    // ……
    // ……
    // ……

    editor.create()
}


//更换验证码

function changeIdentifyingCode(){
	var identifyingcodeURL=getContextPath()+"/kaptcha/getpic";
	$('#identifyingcode').attr('src',identifyingcodeURL);
}

//添加文章 todo

function addArticle(){
	var identifyingCode = $('#code').val(); //验证码
	var addArticleURL=getContextPath()+"/backStage/addArticle";
	var articleName = $('#articleName').val(); //标题
	var articleSortID = $("#articlesort").find("option:selected").attr('sortArticleId'); //分组id
	var articleContent = $("#articleContent").val(); //内容
	var typeId = $("#typeId").find("option:selected").attr('typeId');  //所属栏目
	var articleType = "0"; //是否公开
		if($("#articleType").is(':checked')) {
	    articleType="1";
		}
	    alert(identifyingCode)
		$.post(addArticleURL, {"articleName": articleName,
			"identifyingCode":identifyingcode,
			"articleSortID":articleSortID,
			"articleContent":articleContent,
			"typeId":typeId,
			"articleType":articleType}, function(data, textStatus, xhr) {
			/*optional stuff to do after success */
				alert("success");
				top.location.href=getContextPath()+"/user/goMain";
		});
	

}
/**
 * 获取版块 (articleType)
 */
function getSection(){
	var getSectionURL =getContextPath()+"/front/getallsection";

	$.ajax({
		url: getSectionURL,
		type: 'POST',
	})
	.done(function(data) {
		var obj=eval("("+data+")"); //转换json
		var section = obj.section;
		if(obj.sucess==true){
			var tempHTML="";
			$.map(section, function(item, index) {
				tempHTML+="<option sectionId='"+item.sectionId+"'>"+item.sectionName+"</option>"
				
			});
			$('#typeId').html(tempHTML);
		}
		console.log(data);
	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});
	

}

//获取分组
function getArticleSort(){
	var articlesortURL=getContextPath()+"/backStage/getarticlesort";
	$.ajax({
		url: articlesortURL,
		type: 'POST',
		dataType: 'json'
	})
	.done(function(data) {
		console.log("success");
			var tempHTML="";
		for (var i = 0; i < data.length; i++) {
			 articleSort=data[i];
			 tempHTML+="<option sortArticleId='"+articleSort.sortArticleId+"'>"+articleSort.sort_article_name+"</option>";
		}
		$('#articlesort').html(tempHTML);

	})
	.fail(function() {
		console.log("error");
	})
	.always(function() {
		console.log("complete");
	});

}

//获取根路径
function getContextPath() { 

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}